import { getRenderingEngine } from "@cornerstonejs/core";
import * as cornerstoneTools from "@cornerstonejs/tools";

const { AnnotationTool, annotation, ToolGroupManager } = cornerstoneTools;

const { getToolGroupForViewport } = ToolGroupManager;
const { setAnnotationSelected } = annotation.selection;

const {
	getAnnotation,
	getAnnotations,
	removeAnnotation,
	removeAnnotations,
	removeAllAnnotations
} = annotation.state;

function removeLast(viewportId, renderingEngineId) {
	const renderingEngine = getRenderingEngine(renderingEngineId);
	if (!renderingEngine) {
		return false;
	}

	const viewport = renderingEngine.getViewport(viewportId);
	if (!viewport) {
		return false;
	}

	const toolGroup = getToolGroupForViewport(viewportId, renderingEngineId);

	if (!toolGroup) {
		return false;
	}

	const imageId = viewport.getCurrentImageId();
	const { element } = viewport;
	const tools = toolGroup._toolInstances;
	const annotationsToRemove = [];

	for (const toolName in tools) {
		const toolInstance = tools[toolName];

		if (
			typeof toolInstance.isPointNearTool !== "function" ||
			typeof toolInstance.filterInteractableAnnotationsForElement !== "function"
		) {
			continue;
		}

		const annotations = getAnnotations(toolName, element);

		const interactableAnnotations =
			toolInstance.filterInteractableAnnotationsForElement(element, annotations);

		if (!interactableAnnotations) {
			continue;
		}

		for (const annotation of interactableAnnotations) {
			if (annotation.metadata.referencedImageId === imageId) {
				annotationsToRemove.push(annotation.annotationUID);
			}
			// if (
			// 	toolInstance.isPointNearTool(
			// 		element,
			// 		annotation,
			// 		currentPoints.canvas,
			// 		10,
			// 		interactionType
			// 	)
			// ) {
			// 	annotationsToRemove.push(annotation.annotationUID);
			// }
		}
	}

	// for (const annotationUID of annotationsToRemove) {
	// 	setAnnotationSelected(annotationUID);
	// 	const annotation = getAnnotation(annotationUID);
	// 	AnnotationTool.createAnnotationMemo(element, annotation, {
	// 		deleting: true
	// 	});
	// 	removeAnnotation(annotationUID);
	// }

	const annotationUID = annotationsToRemove[annotationsToRemove.length - 1];
	setAnnotationSelected(annotationUID);

	const annotation = getAnnotation(annotationUID);
	AnnotationTool.createAnnotationMemo(element, annotation, {
		deleting: true
	});

	removeAnnotation(annotationUID);

	return true;
}

function removeAll(viewportId, renderingEngineId) {
	const renderingEngine = getRenderingEngine(renderingEngineId);
	if (!renderingEngine) {
		return false;
	}

	const viewport = renderingEngine.getViewport(viewportId);
	if (!viewport) {
		return false;
	}

	const toolGroup = getToolGroupForViewport(viewportId, renderingEngineId);

	if (!toolGroup) {
		return false;
	}

	const { element } = viewport;
	const imageId = viewport.getCurrentImageId();

	const tools = toolGroup._toolInstances;
	const annotationsToRemove = [];

	for (const toolName in tools) {
		const toolInstance = tools[toolName];

		if (
			typeof toolInstance.isPointNearTool !== "function" ||
			typeof toolInstance.filterInteractableAnnotationsForElement !== "function"
		) {
			continue;
		}

		const annotations = getAnnotations(toolName, element);

		const interactableAnnotations =
			toolInstance.filterInteractableAnnotationsForElement(element, annotations);

		if (!interactableAnnotations) {
			continue;
		}

		for (const annotation of interactableAnnotations) {
			if (annotation.metadata.referencedImageId === imageId) {
				annotationsToRemove.push(annotation.annotationUID);
			}
			// if (
			// 	toolInstance.isPointNearTool(
			// 		element,
			// 		annotation,
			// 		currentPoints.canvas,
			// 		10,
			// 		interactionType
			// 	)
			// ) {
			// 	annotationsToRemove.push(annotation.annotationUID);
			// }
		}
	}

	for (const annotationUID of annotationsToRemove) {
		setAnnotationSelected(annotationUID);
		const annotation = getAnnotation(annotationUID);
		AnnotationTool.createAnnotationMemo(element, annotation, {
			deleting: true
		});
		removeAnnotation(annotationUID);
	}

	// const annotationUID = annotationsToRemove[annotationsToRemove.length - 1];
	// setAnnotationSelected(annotationUID);

	// const annotation = getAnnotation(annotationUID);
	// AnnotationTool.createAnnotationMemo(element, annotation, {
	// 	deleting: true
	// });

	// removeAnnotation(annotationUID);

	return true;
}

export { removeLast, removeAll };
